home *** CD-ROM | disk | FTP | other *** search
- //-----------------------------------------------------------------------
- //
- // SOLUTION: Inventor Barcelona Lab #4
- //
- // This program illustrates a simple Inventor program which reads
- // in an Inventor file and views it with the `Walkthrough viewer'.
- // If the `lamp' is picked then play some audio and turn the light on/off...
- //
- // EXERCISE:
- // When the `Table' is picked, spin the `Lamp' about the center
- // of the table.
- //
- // Hint: Find the SoRotor `TableRotor' in scene.iv and turn it on
- // when the table is picked...
- //
- //-----------------------------------------------------------------------
-
- #include <stdio.h>
- #include <Inventor/SoDB.h>
- #include <Inventor/actions/SoSearchAction.h>
- #include <Inventor/events/SoMouseButtonEvent.h>
- #include <Inventor/nodes/SoEventCallback.h>
- #include <Inventor/nodes/SoSeparator.h>
- #include <Inventor/nodes/SoSwitch.h>
- #include <Inventor/nodes/SoRotor.h>
- #include <Inventor/Xt/SoXt.h>
- #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
-
-
- SoRotor *tableRotor;
- SoSwitch *lampSwitch;
-
- void lampCallback( void *, SoEventCallback *cb ) {
- // Make sure that this is a MOUSE PRESS event
- if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
-
- if ( lampSwitch->whichChild.getValue() == SO_SWITCH_NONE ) {
- // Lamp is OFF - turn it ON...
- lampSwitch->whichChild = 0;
- system( "playaiff sounds/lampON.aiff &" );
- }
- else {
- // Lamp is ON - turn it OFF...
- lampSwitch->whichChild = SO_SWITCH_NONE;
- system( "playaiff sounds/lampOFF.aiff &" );
- }
- }
-
- void lampSwitchInit( SoSeparator *root )
- {
- // Find the `Lamp' and the `Lamp Switch' nods.
- SoGroup *lamp = (SoGroup *) root->getByName( "Lamp" );
- lampSwitch = (SoSwitch *) root->getByName( "LampSwitch" );
-
- if ( (lamp == NULL) || (lampSwitch == NULL)
- || (!lamp->isOfType( SoGroup::getClassTypeId()))
- || (!lampSwitch->isOfType( SoSwitch::getClassTypeId())) ) {
- printf( "DEBUG: `Lamp' or `LampSwitch' not found, or wrong types\n" );
- exit( 0 );
- }
-
- // Get the path to `Lamp'
- SoSearchAction sa;
- sa.setFind( SoSearchAction::NODE );
- sa.setNode( lamp );
- sa.apply( root );
- SoPath *path = sa.getPath();
-
- // Create an event callback node that calls a function if the
- // `Lamp' is picked.
-
- SoEventCallback *lampCB = new SoEventCallback;
- lampCB->setPath(path);
- lampCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
- lampCallback );
- lamp->addChild( lampCB );
- }
-
-
- void tableCallback( void *, SoEventCallback *cb ) {
- // Make sure that this is a MOUSE PRESS event
- if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
-
- if ( tableRotor->on.getValue() ) {
- system( "playaiff sounds/tableOFF.aiff &" );
- tableRotor->on = FALSE;
- }
- else {
- system( "playaiff sounds/tableON.aiff &" );
- tableRotor->on = TRUE;
- }
- }
-
-
- void tableInit( SoSeparator *root )
- {
- // Find the `Table' node
- SoGroup *table = (SoGroup *) root->getByName( "Table" );
- tableRotor = (SoRotor *) root->getByName( "TableRotor" );
-
- if ( (table == NULL) || (!table->isOfType( SoGroup::getClassTypeId())) ) {
- printf( "DEBUG: `Table' not found or wrong type\n" );
- exit( 0 );
- }
-
- // Get the path to `Table'
- SoSearchAction sa;
- sa.setFind( SoSearchAction::NODE );
- sa.setNode( table );
- sa.apply( root );
- SoPath *path = sa.getPath();
-
- // Create an event callback node that calls a function if the
- // `Table' is picked.
-
- SoEventCallback *tableCB = new SoEventCallback;
- tableCB->setPath(path);
- tableCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
- tableCallback );
- table->addChild( tableCB );
- }
-
-
- main(int argc, char **argv)
- {
- // Initialize Inventor
- Widget myWindow = SoXt::init( argv[0] );
- if (myWindow == NULL) exit(1);
-
- // Make a viewer part of the window
- SoXtWalkViewer *viewer = new SoXtWalkViewer(myWindow);
-
- // Read the object from a file
- SoInput myInput;
- if (!myInput.openFile("./scene.iv")) return(1);
- SoSeparator *scene = SoDB::readAll(&myInput);
- if (scene == NULL) {
- printf( "Error: scene.iv file read failed!\n" );
- exit(1);
- }
- scene->ref();
-
- // Search for the different objects by name
- lampSwitchInit( scene );
- tableInit( scene );
-
- // Viewer setup
- viewer->setSceneGraph( scene );
- viewer->setTitle( "Walkthrough Program" );
- viewer->show();
- SoXt::show(myWindow);
-
- SoXt::mainLoop();
- }
-